package com.ghy.web.backend;

import com.ghy.entity.EbProduct;
import com.ghy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "page",defaultValue = "1")int psize,
                       @RequestParam(value = "pageSize",defaultValue = "5")int pageSize){
        System.out.println("start service.....;psize==>"+psize);
        List<EbProduct> productList = productService.listByPage(psize,pageSize);
        int totalPage = productService.getMaxPage(pageSize);
        //2.传递model(数据)和view视图(jsp)
        model.addAttribute("productList",productList);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("curPage",psize);
        return "manage/product";//视图  通过manage/news.jsp进行展示数据
    }
   @RequestMapping("/save")
    public  String Save(HttpServletRequest request,EbProduct ebProduct, MultipartFile productFileName){
        String path = request.getServletContext().getRealPath("/")+"images/product";
        String pictureFile_name = productFileName.getOriginalFilename();
        System.out.println("originFile:"+pictureFile_name);
        String newFileName = UUID.randomUUID().toString()+pictureFile_name.substring(pictureFile_name.lastIndexOf("."));
        File uploadPic = new java.io.File(path+newFileName);
        ebProduct.setEpFileName(newFileName);
        try{
            if(!uploadPic.exists()){
                uploadPic.mkdirs();
            }
            productFileName.transferTo(uploadPic);
        }catch (Exception e){
            e.printStackTrace();

        }
       int result = productService.save(ebProduct);
       if (result<1){
           return "manage/manage-error";
       }
       return "manage/manage-result";
    }
    @RequestMapping("/to_save")
    public String toProductAdd(){
        return "manage/product-add";
    }
   // @RequestMapping("/save_commit")
  //  public String saveCommit(EbProduct ebProduct){
   //     if (ebProduct.getEpName()==null||ebProduct.getEpName().trim().length()==0){
    //        return "manage/paramError";
    //    }
     //   int result = productService.save(ebProduct);
    //    if (result<=0){
     //       return "manage/manage-error";
     //   }
     //   return "manage/manage-result";
  //  }
    @RequestMapping("/delete/{epId}")
    public String delete(@PathVariable("epId") Integer epId){
        int result = productService.delete(epId);
        if(result==0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/edit")
    public String edit(Model model,@RequestParam("epId")int epId){
        System.out.println("点击修改跳过来了，epId="+epId);
        EbProduct ebProduct = productService.select(epId);
        model.addAttribute("ebProduct",ebProduct);
        return "manage/product-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbProduct ebProduct){
        System.out.println("ebProduct==>"+ebProduct);
        Integer result = productService.update(ebProduct);
        if(result<=0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
}
